/**
 * 
 */
package com.netscenery.yang.model;

import org.jdom2.Attribute;
import org.jdom2.Element;

import com.netscenery.util.ErrTag;
import com.netscenery.util.Unicode;
import com.netscenery.util.XMLTag;
import com.netscenery.yang.YangKeyword;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;

/**
 * @author llly
 *
 */
public class Pattern extends ModelElement {
	private String description;
	private String reference;

	
	public Pattern(){
		super();
	}
	
	public Pattern(String name){
		if (null == name){
			return;
		}
		String fixedName = name.replaceAll("\\\\","\\\\\\\\");
		java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(fixedName);
		if (null == pattern){
			return;
		}
		
		this.name = name;
	}
	
	public boolean setName(String name){
		if (null == name){
			return false;
		}
		
		String fixedName = name.replaceAll("\\\\","\\\\\\\\");
		java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(fixedName);
		if (null == pattern){
			return false;
		}
		
		this.name = name;	
		return true;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the reference
	 */
	public String getReference() {
		return reference;
	}

	/**
	 * @param reference the reference to set
	 */
	public void setReference(String reference) {
		this.reference = reference;
	}
	
	public boolean checkValue(String value){
		return java.util.regex.Pattern.matches(name, value);
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#validate()
	 */
	@Override
	public Validator validate() {
		Validator validator = new Validator();
		if (isNullString(name)){
			validator.setIsValid(false);
			validator.addMsg(new Message(this, ErrTag.ERROR,"regular expression must be not null."));
			
		}
		else {
			String fixedName = name.replaceAll("\\\\","\\\\\\\\");
			java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(fixedName);
			if (null == pattern){
				validator.setIsValid(false);
				validator.addMsg(new Message(this, ErrTag.ERROR,"invalid regular expression."));

			}
		}
		if (!isNullString(this.description)){
			if (Unicode.containChinese(this.description)){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,"description",ErrorCode.INVALID_CHARACTER));
			}
		}
		
		if (!isNullString(this.reference)){
			if (Unicode.containChinese(this.reference)){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,"reference",ErrorCode.INVALID_CHARACTER));
			}
		}
		Validator sValidator = super.validate();
		validator.copy(sValidator);
		return validator;
	}
	
	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toYang()
	 */
	@Override
	public YangStatement toYang() {

		YangStatement pattern = new YangStatement(YangStatement.YANG_STATEMENT_PATTERN,this.name);
		pattern.setNeedQuotes(true);
		
		if (null != this.description) {
			YangStatement descr = new YangStatement(YangStatement.YANG_STATEMENT_DESCRIPTION,this.description);
			pattern.addChild(descr);
		}
		
		if (null != this.reference){
			YangStatement ref = new YangStatement(YangStatement.YANG_STATEMENT_REFERENCE,this.reference);
			pattern.addChild(ref);		
		}
		return pattern;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toXML()
	 */
	@Override
	public Element toXML() {
	
		
		Element pattern = new Element(XMLTag.XML_TAG_PATTERN);
		
			Element regstr = new Element(XMLTag.XML_TAG_REGSTR);
			regstr.setText(this.name);
			pattern.addContent(regstr);
		
		
		
		/*
		 * ����
		 */
		if (null != this.description){
			Element descr = new Element(XMLTag.XML_TAG_DESCRIPTION);
			descr.setText(this.description);
			pattern.addContent(descr);
		}	
		
		/*
		 * �ο�
		 */
		if (null != this.reference){
			Element ref = new Element(XMLTag.XML_TAG_REFERENCE);
			ref.setText(this.reference);
			pattern.addContent(ref);
		}	
		return pattern;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#isFieldMandatory(java.lang.String)
	 */
	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readXML(org.jdom2.Element)
	 */
	@Override
	public int readXML(Element element) {
		if (null == element){
			return MODELELEMENT_OK;
		}

		String value = element.getText();
		if (element.getName().equals(XMLTag.XML_TAG_DESCRIPTION)){
			this.setDescription(value);
		}
		else if (element.getName().equals(XMLTag.XML_TAG_REFERENCE)){
			this.setReference(value);
		}
		else if (element.getName().equals(XMLTag.XML_TAG_REGSTR)){
			return MODELELEMENT_OK;
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYANG(com.netscenery.modeldesigner.util.YangStatement)
	 */
	@Override
	public int readYANG(YangStatement statement) {
		if (null == statement){
			return MODELELEMENT_OK;
		}

		String value = statement.getArgument();
		if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			this.setDescription(value);
		}
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			this.setReference(value);
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}

	
	public  MapInfo getMapInfo() throws SecurityException, NoSuchMethodException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Element toYin() {
		Element pattern = new Element(YangStatement.YANG_STATEMENT_PATTERN,env.getNs());
		Attribute value = new Attribute(YangKeyword.VALUE.getKeyword(),this.name);

		pattern.setAttribute(value);
		if (!isNullString(this.getDescription())){
			Element descr = new Element(YangStatement.YANG_STATEMENT_DESCRIPTION,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.description);
			descr.addContent(text);
			pattern.addContent(descr);
			
		}
		
		if (!isNullString(this.getReference())){
			Element ref = new Element(YangStatement.YANG_STATEMENT_REFERENCE,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.reference);
			ref.addContent(text);
			pattern.addContent(ref);
		}
		return pattern;
	}

	@Override
	public int readYin(Element statement) throws ModelException {
		if (null == statement){
			return MODELELEMENT_OK;
		}

		
		// ����
		if (statement.getName().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)) {
			Element text = statement.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setDescription(text.getText());
		}
		// reference
		else if (statement.getName().equals(
				YangStatement.YANG_STATEMENT_REFERENCE)) {
			Element text = statement.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setReference(text.getText());
		}
		else {
			return MODELELEMENT_FAIL;
		}
		
		return MODELELEMENT_OK;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

}
